//
//  BaseView.m
//  OCStarterProject
//
//  Created by huaya on 2025/10/2.
//

#import "BaseView.h"

@interface BaseView ()

@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;
@property (nonatomic, strong) UIView *loadingView;
@property (nonatomic, strong) UILabel *emptyLabel;
@property (nonatomic, strong) UILabel *errorLabel;

@end

@implementation BaseView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self setupConstraints];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    
    // 初始化加载视图
    _loadingView = [[UIView alloc] init];
    _loadingView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    _loadingView.hidden = YES;
    
    _loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    _loadingIndicator.color = [UIColor whiteColor];
    
    [_loadingView addSubview:_loadingIndicator];
    [self addSubview:_loadingView];
    
    // 初始化空状态视图
    _emptyLabel = [[UILabel alloc] init];
    _emptyLabel.textAlignment = NSTextAlignmentCenter;
    _emptyLabel.textColor = [UIColor lightGrayColor];
    _emptyLabel.font = [UIFont systemFontOfSize:16];
    _emptyLabel.numberOfLines = 0;
    _emptyLabel.hidden = YES;
    [self addSubview:_emptyLabel];
    
    // 初始化错误状态视图
    _errorLabel = [[UILabel alloc] init];
    _errorLabel.textAlignment = NSTextAlignmentCenter;
    _errorLabel.textColor = [UIColor redColor];
    _errorLabel.font = [UIFont systemFontOfSize:16];
    _errorLabel.numberOfLines = 0;
    _errorLabel.hidden = YES;
    [self addSubview:_errorLabel];
}

- (void)setupConstraints {
    // 加载视图约束
    _loadingView.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [_loadingView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
        [_loadingView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
        [_loadingView.topAnchor constraintEqualToAnchor:self.topAnchor],
        [_loadingView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor]
    ]];
    
    // 加载指示器约束
    _loadingIndicator.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [_loadingIndicator.centerXAnchor constraintEqualToAnchor:_loadingView.centerXAnchor],
        [_loadingIndicator.centerYAnchor constraintEqualToAnchor:_loadingView.centerYAnchor]
    ]];
    
    // 空状态标签约束
    _emptyLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [_emptyLabel.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
        [_emptyLabel.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
        [_emptyLabel.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.leadingAnchor constant:20],
        [_emptyLabel.trailingAnchor constraintLessThanOrEqualToAnchor:self.trailingAnchor constant:-20]
    ]];
    
    // 错误状态标签约束
    _errorLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [_errorLabel.centerXAnchor constraintEqualToAnchor:self.centerXAnchor],
        [_errorLabel.centerYAnchor constraintEqualToAnchor:self.centerYAnchor],
        [_errorLabel.leadingAnchor constraintGreaterThanOrEqualToAnchor:self.leadingAnchor constant:20],
        [_errorLabel.trailingAnchor constraintLessThanOrEqualToAnchor:self.trailingAnchor constant:-20]
    ]];
}

- (void)updateWithData:(id)data {
    // 子类重写此方法
}

#pragma mark - Loading Methods

- (void)showLoading {
    _loadingView.hidden = NO;
    [_loadingIndicator startAnimating];
    [self bringSubviewToFront:_loadingView];
}

- (void)hideLoading {
    _loadingView.hidden = YES;
    [_loadingIndicator stopAnimating];
}

#pragma mark - Empty State Methods

- (void)showEmptyWithMessage:(NSString *)message {
    _emptyLabel.text = message;
    _emptyLabel.hidden = NO;
    [self bringSubviewToFront:_emptyLabel];
}

- (void)hideEmpty {
    _emptyLabel.hidden = YES;
}

#pragma mark - Error State Methods

- (void)showErrorWithMessage:(NSString *)message {
    _errorLabel.text = message;
    _errorLabel.hidden = NO;
    [self bringSubviewToFront:_errorLabel];
}

- (void)hideError {
    _errorLabel.hidden = YES;
}

@end
